<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    {$LOAD_STYLE.top|raw}
<style>
    .color-box{
        width: 40px;
        height: 40px;
        border-radius: 5px;
        margin: 0px;
    }
    .pur1{
        color:#36b368;
    }
    .pur2{
        color:#2d8cf0;
    }
    .pur3{
        color:#8799a3;
    }
    .tr1 td{
    	background-color: #f8fbff;
    }
    .tr2 td{
    	background-color: #fffffb;
    }
</style>
<script src="__JS__/clipboard.min.js"></script>
</head>
	<body class="pear-container">
		<div class="layui-card">
			<div class="layui-card-body">
				<form class="layui-form" action="">
                    <div class="layui-form-item layui-form-pane">
                       
                       <div class="layui-form-item layui-inline">
                            <label class="layui-form-label">面料编号</label>
                           <div class="layui-input-inline">
                               <input type="text" name="fabric_code" placeholder="面料编号" class="layui-input">
                           </div>
                       </div>
                       
                       <div class="layui-form-item layui-inline">
                            <label class="layui-form-label">面料色号</label>
                           <div class="layui-input-inline">
                               <input type="text" name="color_code" placeholder="面料色号" class="layui-input">
                           </div>
                       </div>
                       
                       <div class="layui-form-item layui-inline">
                            <label class="layui-form-label">供应编号</label>
                           <div class="layui-input-inline">
                               <input type="text" name="sup_code" placeholder="供应编号" class="layui-input">
                           </div>
                       </div>
                       
                       <div class="layui-form-item layui-inline">
                            <label class="layui-form-label">供应色号</label>
                           <div class="layui-input-inline">
                               <input type="text" name="sup_color" placeholder="供应色号" class="layui-input">
                           </div>
                       </div>
                       
                       <div class="layui-form-item layui-inline">
                            <label class="layui-form-label">供应商</label>
                           <div class="layui-input-inline">
                               <input type="text" name="sup_name" placeholder="供应商" class="layui-input">
                           </div>
                       </div>
                       
                    </div>
                    
					<div class="layui-form-item layui-form-pane">
						
						<div class="layui-form-item layui-inline">
							<select name="year">
								<option value="">选择年份</option>
								{for start="$old_year" end="date('Y')+1"}
									{if $year == $i}
									<option value="{$i}" selected="selected">{$i}年</option>
									{else/}
									<option value="{$i}">{$i}年</option>
									{/if}
								{/for}
							</select>
						</div>
						
						<div class="layui-form-item layui-inline">
							<select name="month">
								<option value="">选择月份</option>
								{for start="1" end="13"}
									{if $month == $i}
									<option value="{$i}" selected="selected">{$i}月</option>
									{else/}
									<option value="{$i}">{$i}月</option>
									{/if}
								{/for}
							</select>
						</div>
						
						<div class="layui-form-item layui-inline">
							<select name="day">
								<option value="">选择天数</option>
								{for start="1" end="32"}
									{if $month == $i}
									<option value="{$i}" selected="selected">{$i}日</option>
									{else/}
									<option value="{$i}">{$i}日</option>
									{/if}
								{/for}
							</select>
						</div>
                        
                        <div class="layui-form-item layui-inline">
                            <select name="exp_method">
                                <option value="">选择提货方式</option>
                                {foreach name="options.exp_method" item="v"}
                                    <option value="{$v.name}">{$v.name}</option>
                                {/foreach}
                            </select>
                        </div>
                        
                        <div class="layui-form-item layui-inline">
                            <select name="exp_name">
                                <option value="">选择物流</option>
                                {foreach name="options.exp_name" item="v"}
                                    <option value="{$v.name}">{$v.name}</option>
                                {/foreach}
                            </select>
                        </div>
					</div>
                    
                    <div class="layui-form-item layui-form-pane">
                        
                        <div class="layui-form-item layui-inline">
                             <label class="layui-form-label">业务员</label>
                            <div class="layui-input-inline">
                                <input type="text" name="staff_name" placeholder="业务员" class="layui-input">
                            </div>
                        </div>
                        
                        <div class="layui-form-item layui-inline">
                            <select name="pur_kind">
                                <option value="">选择采购类型</option>
                                <option value="1">版布</option>
                                <option value="2">大货</option>
                                <option value="3">A4</option>
                                <option value="4">公斤</option>
                                <option value="5">大货样</option>
                            </select>
                        </div>

                        <div class="layui-form-item layui-inline">
                            <select name="pur_status">
                                <option value="">选择订单状态</option>
                                {foreach name="options.pur_status" item="v"}
                                    <option value="{$v.level}">{$v.name}</option>
                                {/foreach}
                            </select>
                        </div>
                        
                        <input type="hidden" name="nodeal" value="0"/>
                        
                        <div class="layui-form-item layui-inline">
                           <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="query">
                               <i class="layui-icon layui-icon-search"></i>
                               查询
                           </button>
                           <button type="reset" class="pear-btn pear-btn-md">
                               <i class="layui-icon layui-icon-refresh"></i>
                               重置
                           </button>
                           <button id="nodeal" type="button" class="pear-btn pear-btn-md">
                               <i class="layui-icon layui-icon-search"></i>
                               未完成订单
                           </button>
                           <button class="pear-btn pear-btn-md" id="settype" data-dropdown="pur_status">
                               批量设置状态
                               <i class="layui-icon layui-icon-down layui-font-12"></i>
                           </button>
                           </div>
                    </div>
				</form>
			</div>
		</div>
		<div class="layui-card">
			<div class="layui-card-body">
				<table id="dataTable" lay-filter="dataTable"></table>
			</div>
		</div>

		<script type="text/html" id="toolbar">
		    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
		        <i class="layui-icon layui-icon-add-1"></i>新增
		    </button>
		    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
		        <i class="layui-icon layui-icon-delete"></i>删除
		    </button>
		    <button class="pear-btn pear-btn-md" lay-event="recycle">
		        <i class="layui-icon layui-icon-delete"></i>回收站
		    </button>
            <button class="pear-btn pear-btn-ext1 pear-btn-md toolbar-btn-left" lay-event="prints" poptitle="合并打印">
                <i class="layui-icon layui-icon-print"></i>
                合并打印
            </button>
		</script>
      
        <script type="text/html" id="pur_kind">
            {{# if(d.pur_kind==1){ }}
            <span class="pur1">版布</span>
            {{# }else if(d.pur_kind==2){ }}
            <span class="pur2">大货</span>
            {{# }else if(d.pur_kind==3){ }}
            <span class="pur3">A4</span>
            {{# }else if(d.pur_kind==4){ }}
            <span class="pur3">KG</span>
            {{# }else if(d.pur_kind==5){ }}
            <span class="pur3">大货样</span>
            {{# } }}
		</script>
        
        <script type="text/html" id="exp_method">
            {{# if(d.exp_method=='二批发货'){ }}
            <span>二批</span>
            {{# }else if(d.exp_method=='中大提发'){ }}
            <span>中大</span>
            {{# }else if(d.exp_method=='外调发货'){ }}
            <span>外调</span>
            {{# } }}
        </script>
        
        <script type="text/html" id="fabric_code">
            <span copy="{{d.delivery}}" title="{{d.delivery}}">{{d.fabric_code}}</span>
        </script>
        
        <script type="text/html" id="fabric_name">
            <span copy="{{d.delivery}}" title="{{d.delivery}}">{{d.fabric_name}}</span>
        </script>
        
        <script type="text/html" id="color_code">
            <span copy="{{d.sketch}}" title="{{d.sketch}}">{{d.color_code}}</span>
        </script>
        
        <script type="text/html" id="exp_name">
            <span copy="{{d.pick_info}}" title="{{d.pick_info}}">{{d.exp_name}}</span>
        </script>
        
        <script type="text/html" id="sup_empty">
            {{# if(d.pur_kind==2){ }}
            <span class="pur2">{{d.sup_empty}}</span>
            {{# } }}
        </script>

        <script type="text/html" id="proof">
            <div class="layui-row">
                {{# if(d.unit=='条' || d.cut_payment==2 && (d.unit=='米' || d.unit=='KG')){ }}
                <input type="checkbox" name="proof" value="{{d.info_id}}" lay-skin="switch" lay-text="已打印|未打印" lay-filter="proof" {{# if(d.proof==1){ }} checked {{# } }}>
                {{# } }}
            </div>
        </script>
        
        <script type="text/html" id="status">
        <div>
            {{# if(d.status==0){ }}
            <span class="layui-tag pear-btn-grey">草稿</span>
        	{{# }else if(d.status==1){ }}
                {{# if(d.pur_type==0){ }}
                <span class="layui-tag pear-btn-orange">报公斤</span>
                {{# }else{ }}
                <span class="layui-tag pear-btn-cyan">报单</span>
                {{# } }}
            {{# }else if(d.status==99){ }}
            <span class="layui-tag pear-btn-green">完成</span>
            {{# }else if(d.status==-1){ }}
            <span class="layui-tag pear-btn-red">取消</span>
        	{{# }else{ }}
        	<span class="layui-tag pear-btn-grey">其他</span>
        	{{# } }}
        </div>
        </script>
        
        <script type="text/html" id="color_hex">
            {{#  if(d.color_hex){ }}
            <div class="color-box" style="background-color:#{{ d.color_hex }}"></div>
            {{#  } }}
        </script>
        
		<script type="text/html" id="options">
            <button class="pear-btn pear-btn-cyan pear-btn-sm" lay-event="pop" url="{:U('purchase.main/info')}/id/{{d.id}}" poptitle="订单明细"><i class="layui-icon layui-icon-list"></i></button>
            <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></button>
		    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i></button>
		</script>
        <script>
        layui.use(['jquery','common','mycomm','table','form'], function() {
            let $ = layui.jquery;
			let common = layui.common;
			let mycomm = layui.mycomm;
			let table = layui.table;
            let form = layui.form;
            let MODULE_PATH = "{$MODULE_PATH}";
            let table_name = "dataTable";
            
            let cols = [
                [{
                      type: 'checkbox',
                    }, {
                       field: "id",
                       title: "ID",
                       width: 80,
                       unresize: "true",
                       hide:"true",
                       align: "center"
                    }, {
                       field: "create_time2",
                       title: "下单时间",
                       width: 110,
                       unresize: "true",
                       align: "center",
                    }, {
                       field: "pur_sn",
                       title: "订单号",
                       hide:true,
                       width: 130,
                       unresize: "true",
                       align: "center",
                    }, {
                       field: "store_name",
                       title: "门店",
                       hide:true,
                       width: 70,
                       unresize: "true",
                       align: "center"
                    }, {
                       field: "staff_name",
                       title: "业务",
                       width: 60,
                       unresize: "true",
                       align: "center",
                    }, {
                       field: "pur_kind",
                       title: "类型",
                       width: 80,
                       unresize: "true",
                       align: "center",
                       templet : "#pur_kind",
                    }, {
                       field: "fabric_code",
                       title: "编号",
                       width: 100,
                       unresize: "true",
                       align: "center",
                       templet : "#fabric_code",
                    }, {
                       field: "fabric_name",
                       title: "品名",
                       width: 150,
                       unresize: "true",
                       align: "left",
                       templet : "#fabric_name",
                    }, {
                       field: "color_code",
                       title: "色号",
                       width: 60,
                       unresize: "true",
                       align: "center",
                       templet : "#color_code",
                    }, {
                       field: "sup_empty",
                       title: "来货空差",
                       width: 90,
                       hide:true,
                       unresize: "true",
                       align: "center",
                       templet : "#sup_empty",
                    }, {
                       field: "color_name",
                       title: "颜色",
                       width: 80,
                       unresize: "true",
                       align: "center",
                    }, {
                       field: "count",
                       title: "数量",
                       width: 70,
                       templet: "#count",
                       unresize: "true",
                       align: "center",
                       templet : "<div>{{d.count}} {{d.unit}}</div>",
                    }, {
                        field: "",
                        title: "",
                        width:  70,
                        hide:true,
                        unresize: "true",
                        align: "left",
                        templet:"#color_hex",
                    }, {
                        field: "sup_name",
                        title: "供应商",
                        width:90,
                        unresize: "true",
                        align: "center",
                    }, {
                      field: "sup_code",
                      title: "供编",
                      width:90,
                      unresize: "true",
                      align: "center",
                    }, {
                      field: "sup_color",
                      title: "供色",
                      width:60,
                      unresize: "true",
                      align: "center",
                   
                    }, {
                       field: "exp_method",
                       title: "发货",
                       width:60,
                       unresize: "true",
                       align: "center",
                       templet : "#exp_method",
                    }, {
                        field: "exp_name",
                        title: "物流",
                        width:80,
                        unresize: "true",
                        align: "center",
                        templet : "#exp_name",
                   }, {
                       field: "status",
                       title: "状态",
                       width:100,
                       unresize: "true",
                       align: "center",
                       templet : "#status",
                   }, {
                       field: "remark",
                       title: "备注",
                       edit:true,
                       unresize: "true",
                       align: "center",
                   }, {
                       field: "proof",
                       title: "单据",
                       width:100,
                       unresize: "true",
                       align: "center",
                       templet : "#proof",
                   }, {
                        title: '操作',
                        toolbar: '#options',
                        width:160,
                        unresize: true,
                        align: 'center',
                    }
                ]
            ]
            
            mycomm.tableTool('{$controller_title}',MODULE_PATH,table_name);
            mycomm.tableToolBar('{$controller_title}',MODULE_PATH,table_name);
            mycomm.search_table(table_name);
			
            table.render({
            	elem: '#'+table_name,
            	url: MODULE_PATH+'index',
            	where:{},
            	page: true,
            	cols: cols,
            	cellMinWidth: 100,
            	even: true,
            	limit: 30,
            	toolbar: '#toolbar',
            	defaultToolbar: [{
            		title: '刷新',
            		layEvent: 'refresh',
            		icon: 'layui-icon-refresh',
            	}, 'filter', 'print', 'exports'],
            	done:function(res,curr,count){
            		var now=0;
            		var tr="tr1";

            		var $wz=5;
                    
            		if(res.data!=undefined){
            			for (let i=0; i < res.data.length; i++) {
            				if(now==0){
            					now=res.data[i].pur_id;
            				}else if(now!=res.data[i].pur_id){
            					tr=tr=="tr1"?"tr2":"tr1";
            					now=res.data[i].pur_id;
            				}
            				$('[data-index='+i+']').addClass(tr);
            			};
            		}
            	}
            });
            
            mycomm.tableToolBar('{$controller_title}',MODULE_PATH,table_name,function(obj){
            	if(obj.event === "prints"){
            		let data = table.checkStatus(obj.config.id).data;
            		if (data.length === 0) {
            			Alert('未选中数据');
            		    return false;
            		}
            		var ids = []
            		var hasCheck = table.checkStatus(table_name)
            		var hasCheckData = hasCheck.data
            		if (hasCheckData.length > 0) {
            		    $.each(hasCheckData, function (index, element) {
            		        ids.push(element.id)
            		    })
            		}
            		ids=ids.join('|');
            		var url = MODULE_PATH + "prints?ids=" + ids;
            		mycomm.tool_pop(url,"合并订单明细",0.4);
            	}
            });
            
            form.on("switch(proof)", function(data) {
                var status = data.elem.checked?1:0;
                var id = this.value;
                var load = layer.load();
                $.post(MODULE_PATH + "status",{proof:status,id:id,type:'info'},function (res) {
                    layer.close(load);
                    //判断有没有权限
                    if(res && res.code==999){
                        layer.msg(res.msg, {
                            icon: 5,
                            time: 2000, 
                        })
                        return false;
                    }else if (res.code==200){
                        Alert(res.msg,'success',1);
                    } else {
                        Alert(res.msg,'error',3,function(){
                            $(data.elem).prop("checked",!$(data.elem).prop("checked"));
                            form.render()
                        });
                    }
                })
            });
            
            //修改信息
            table.on('edit('+table_name+')', function(obj) {
            	let value = obj.value;	//修改后的数据
            	let field = obj.field;	//修改的字段名
            	let data = obj.data;	//修改的当行数据
            	let id = data.info_id;
            	let loading = layer.load();
            	let td = $(obj.tr.selector +" [data-field=" + field + "]");
                $.ajax({
                    url:MODULE_PATH + 'setValue',
                    data:{
            			id:id,
            			field:field,
            			value:value,
                        type:'info'
            		},
                    dataType: 'json',
                    type: 'POST',
                    success: function(res) {
                        layer.close(loading);
                        //判断有没有权限
                        if(res && res.code==999){
                            Alert(res.msg);
            				td.addClass('pear-btn-danger');
                            return false;
                        }else if (res.code==200) {
                            //Alert(res.msg,'success',1);
            				td.addClass('pear-btn-primary');
                        } else {
                            Alert(res.msg,'error',3);
            				td.addClass('pear-btn-danger');
                        }
                    }
                })
            });
            
            $(document).ready(function(){
                $("#nodeal").click(function(){
                	$('[type="reset"]').click();
                    $('[name="nodeal"]').val(1);
                    $(this).addClass("pear-btn-primary");
                    $("[lay-submit]").click();
                });
                $('[type="reset"]').click(function(){
                    $('[name="nodeal"]').val(0);
                    $('#nodeal').removeClass("pear-btn-primary");
               });
               initDropdown();
            })
            
            // 是否选中产品
            function isSelect(table,table_name){
            	let data = table.checkStatus(table_name).data;
            	if (data.length === 0) {
            		Alert('未选中数据','info',1.5);
            		return false;
            	}
            	var ids = [];
            	var hasCheck = table.checkStatus(table_name)
            	var hasCheckData = hasCheck.data
            	if (hasCheckData.length > 0) {
            		$.each(hasCheckData, function (index, element) {
            			ids.push(element.id)
            		})
            	}
            	ids=ids.join('|');
            	return ids;
            }
            
            // 初始化下拉菜单
            function initDropdown() {
                var dropdownData =[
                    {volist name="options.pur_status" id="v"}
                    {
                        "title": "{$v.name}",
                        "id": {$v.level}
                    },
                    {/volist}
                ];
                
                layui.dropdown.render({
                    elem: '#settype',
                    data: dropdownData,
                    click: function(obj){
                        let ids = isSelect(table, table_name);
                        if(ids){
                            confirmed('确定设置为【'+obj.title+'】状态吗？', function(){
                                $.post("{:U('purchase.main/status')}", {
                                    type: 'more_status',
                                    status: obj.id,
                                    id: ids
                                }, function(res){
                                    if(res.code == 200){
                                        Alert('修改成功！','success',1);
                                         table.reload(table_name);
                                    }else{
                                        Alert(res.msg,'error',3);
                                    }
                                });
                            });
                        }
                    }
                });
            }
        })
    </script>
	{$LOAD_STYLE.bottom|raw}
	</body>
</html>
